//
//  DCSearchWithVINView.m
//  JiuyaoDriver
//
//  Created by 大橙子 on 2018/11/22.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCSearchWithVINView.h"

@interface DCSearchWithVINView()<UITextFieldDelegate>
{
    UIButton *qCode;
}
@property (nonatomic, strong)UIButton *search;
@property (nonatomic,copy) scanBtnDidClick scanBtnDidClick;
@property (nonatomic,copy) searchBtnDidClick searchBtnDidClick;
@end
@implementation DCSearchWithVINView

-(instancetype)initWithFrame:(CGRect)frame withScanBtnblock:(scanBtnDidClick)scanBtnblock searchBtnBlock:(searchBtnDidClick)searchBtnBlock
{
    if (self = [super initWithFrame:frame]) {
        self.scanBtnDidClick = scanBtnblock;
        self.searchBtnDidClick = searchBtnBlock;
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI
{
    _textField = [[UITextField alloc]init];
    _textField.placeholder = @" 请输入VIN码";
    //    _textField.text = @"LBE2DAFB1JZ029450";
    _textField.keyboardType = UIKeyboardTypeASCIICapable;
    _textField.font = FONT(14);
    _textField.returnKeyType = UIReturnKeySearch;
    _textField.rightViewMode = UITextFieldViewModeAlways;
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.delegate = self;
    [self addSubview:_textField];
    
    qCode = [UIButton buttonWithType:UIButtonTypeCustom];
    [qCode setImage:IMAGENAME(@"scan") forState:UIControlStateNormal];
    [qCode addTarget:self action:@selector(qCodeClick:) forControlEvents:UIControlEventTouchUpInside];
    _textField.rightView = qCode;
    
    _search = [[UIButton alloc]init];
    [_search setTitle:@"搜索" forState:UIControlStateNormal];
    [_search setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _search.layer.cornerRadius = 5;
    _search.layer.masksToBounds = YES;
    _search.titleLabel.font = FONT(15);
    [_search addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _search.backgroundColor = BlueColor;
    [self addSubview:_search];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _search.frame = CGRectMake(self.width - 70, 3, 70, self.height-6);
    _textField.frame = CGRectMake(0, 0, self.width-80, self.height);
    qCode.frame = CGRectMake(0, (self.height - 25)/2, 25, 25);
    
}
- (void)qCodeClick:(UIButton *)sender
{
    //条形码
    __weak typeof(self) weakself = self;
    DCQRScanVC *scanVc = [DCQRScanVC ScanWithUIType:ScanUITypeZhiFuBao codeType:ScanCodeTypeBar scanType:ScanOtherType doneBlock:^(NSString *resultStr) {
        weakself.textField.text = resultStr;
    }];
    if (self.scanBtnDidClick) {
        self.scanBtnDidClick(scanVc,self.textField.text);
    }
}

- (void)searchBtnClick
{
    if (self.textField.text.length == 0) {
        [[UIViewController currentViewController] showMessage:@"请输入VIN码" afterDelay:1.0];
        return;
    }
    if (self.searchBtnDidClick) {
        self.searchBtnDidClick(self.textField.text);
    }
    self.textField.text = nil;
    [self.textField endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self searchBtnClick];
    return YES;
}

@end
